<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-20
 * Time: 下午4:17
 */
 
class BaoBeiSourceModel extends Model{

    public function get($id){

        $sql = "select * from baobeisource where id = :id";

        return $this->db()->getRow($sql,array('id'=>$id));
    }

    public function getBaoBeiSourceList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['en_name'])){
            $where .= ' and en_name like :en_name';
            $bindarr['en_name'] = '%'.$filter['en_name'].'%';
        }

        if(isset($filter['zh_name'])){
            $where .= ' and zh_name like :zh_name';
            $bindarr['zh_name'] = '%'.$filter['zh_name'].'%';
        }

        if(isset($filter['is_show'])){
            $where .= ' and is_show = :is_show';
            $bindarr['is_show'] = $filter['is_show'];
        }

        $ret = $this->pager($curr_page, $per_page)
                ->ct('select count(*) from baobeisource where ' . $where , $bindarr)
                ->rs('select * from baobeisource where '.$where.' order by id asc',$bindarr);
        return $ret;
    }
    
    public function addBaoBeiSource($zh_name,$en_name,$is_show,$icon_css){

        $sql = "insert into baobeisource(zh_name,en_name,is_show,icon_css,add_time) values(:zh_name,:en_name,:is_show,:icon_css,now())";

        $bindarr = array(
            'zh_name'=>$zh_name,
            'en_name'=>$en_name,
            'is_show'=>$is_show,
            'icon_css'=>$icon_css
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    public function updateBaoBeiSource($id,$zh_name,$en_name,$is_show,$icon_css){

        $sql = "update baobeisource set zh_name = :zh_name,is_show = :is_show,en_name = :en_name,icon_css = :icon_css where id = :id";

        $bindarr = array(
            'id'=>$id,
            'is_show'=>$is_show,
            'zh_name'=>$zh_name,
            'en_name'=>$en_name,
            'icon_css'=>$icon_css
        );

        return $this->db()->execute($sql,$bindarr);
    }

    public function getAllSourceList(){
        $sql = "select * from baobeisource";
        return $this->db()->getAll($sql);
    }
}